import 'package:flutter/material.dart';
import 'package:flutterapp/src/home_page.dart';
import 'package:flutterapp/src/utils/navigator_utils.dart';

class FirstGuildPage extends StatefulWidget {
  @override
  _FirstGuildPageState createState() => _FirstGuildPageState();
}

class _FirstGuildPageState extends State<FirstGuildPage> {
  int _currentIndex = 0;
  List<String> banners = [
    'assets/images/banner/banner01.jpg',
    'assets/images/banner/banner04.jpg',
    'assets/images/banner/banner05.jpg',
    'assets/images/banner/banner16.jpg'
  ];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: Stack(
        children: [
          // 第一层照片
          buildBackground(),
          // 第二层指示点
          buildIndexDot(),
          // 第三层 去首页
          buildGoHome(),
        ],
      ),
    );
  }

  // 构建背景banner
  Widget buildBackground() {
    double width = MediaQuery.of(context).size.width;
    double height = MediaQuery.of(context).size.height;
    return PageView(
      onPageChanged: (value) {
        setState(() {
          _currentIndex = value;
        });
      },
      children: banners
          .map((e) => Image.asset(
                e,
                width: width,
                height: height,
                fit: BoxFit.fill,
              ))
          .toList(),
    );
  }

  // 构建指示点
  Widget buildIndexDot() {
    if (_currentIndex == banners.length - 1) return Container();
    return Positioned(
      left: 0,
      right: 0,
      bottom: 66,
      height: 44,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          buildIndexDotItem(0),
          buildIndexDotItem(1),
          buildIndexDotItem(2),
          buildIndexDotItem(3),
        ],
      ),
    );
  }

  Widget buildIndexDotItem(int index) {
    bool isSelect = index == _currentIndex;
    return AnimatedContainer(
      duration: Duration(milliseconds: 800),
      height: 12,
      width: isSelect ? 40 : 12,
      margin: EdgeInsets.only(left: 16),
      decoration: BoxDecoration(
        color: Colors.deepOrangeAccent,
        borderRadius: BorderRadius.all(Radius.circular(10)),
      ),
    );
  }

  // 构建去首页按钮
  Widget buildGoHome() {
    bool isGoHome = _currentIndex == banners.length - 1;
    print(isGoHome);
    return Positioned(
      left: 0,
      right: 0,
      bottom: 66,
      height: 44,
      child: Row(mainAxisAlignment: MainAxisAlignment.center, children: [
        AnimatedContainer(
          duration: Duration(milliseconds: 600),
          height: isGoHome ? 44 : 0,
          width: isGoHome ? 180 : 0,
          margin: EdgeInsets.only(left: 16),
          child: ElevatedButton(
            child: Text("上岸"),
            onPressed: () {
              NavigatorUtils.pushPageByFade(
                context: context,
                targetPage: HomePage(),
                isReplace: true,
              );
            },
          ),
        ),
      ]),
    );
  }
}
